package com.ls.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ls.domain.Teacher;
import com.ls.service.TeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    //=====通用
    //登录
    @PostMapping("/login")
    public Teacher login(HttpServletRequest request, @RequestBody Teacher teacher){
        LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teacher::getTno, teacher.getTno())
                .eq(Teacher::getTeacherPassword,teacher.getTeacherPassword());
        Teacher tch = teacherService.getOne(queryWrapper);
        request.getSession().setAttribute("teacher",tch.getId());
        return tch;
    }

    //登出
    @PostMapping("/logout")
    public boolean logout(HttpServletRequest request){
        request.getSession().removeAttribute("teacher");
        return true;
    }

    //增加教师
    @PostMapping("/save")
    public boolean save(@RequestBody Teacher teacher){
        return teacherService.save(teacher);
    }

    //根据id删除教师
    @DeleteMapping("/deleteById/{id}")
    public boolean deleteById(@PathVariable Integer id){
        return teacherService.removeById(id);
    }

    //根据ids批量删除教师
    @PostMapping("/deleteByIds")
    public boolean deleteByIds(@RequestBody List<Integer> ids){
        return teacherService.removeByIds(ids);
    }

    //根据id和实体修改教师
    @PutMapping("/update/{id}")
    public boolean update(@RequestBody Teacher teacher){
        return teacherService.updateById(teacher);
    }

    //学生改密码
    @PutMapping("/password/{id}")
    public boolean password(@RequestBody Teacher teacher) {
        return teacherService.updateById(teacher);
    }

    //重置学生密码
    @GetMapping("/resetPassword/{id}")
    public boolean resetPassword(@PathVariable("id") Integer id) {
        Teacher teacher = teacherService.getById(id);
        teacher.setTeacherPassword("123456");
        return teacherService.updateById(teacher);
    }

    //根据id查教师
    @GetMapping("/selectById/{id}")
    public Teacher selectById(@PathVariable("id") Integer id){
        return teacherService.getById(id);
    }

    //管理员查教师
    @GetMapping("/selectAll")
    public Map<String, Object> selectAll(int currentPage, int pageSize){
        Page<Teacher> pageInfo = new Page<>(currentPage, pageSize);
        teacherService.page(pageInfo);
        Map<String, Object> map = new HashMap<>();
        map.put("tableData", pageInfo.getRecords());
        map.put("totalCount", pageInfo.getTotal());
        return map;
    }

    //管理员搜索教师 教师号、教师姓名、班级、系
    @PostMapping("/selectByConditions")
    public Map<String, Object> selectByConditions(@RequestBody Teacher teacher,int currentPage, int pageSize){
        LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Teacher::getTno,teacher.getTno())
                .like(Teacher::getTeacherName,teacher.getTeacherName())
                .like(Teacher::getTeacherClass,teacher.getTeacherClass())
                .like(Teacher::getTeacherDepartment,teacher.getTeacherDepartment());
        Page<Teacher> pageInfo = new Page<>(currentPage, pageSize);
        teacherService.page(pageInfo, queryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("tableData", pageInfo.getRecords());
        map.put("totalCount", pageInfo.getTotal());
        return map;
    }
}
